import Vue from "vue";
import Router from "vue-router";
Vue.use(Router);

const routes = [
  {
    path:'/',
    name: '',
    redirect:'/login'
  },
  {
    path:'/login',
    component:() =>import ('@/views/login.vue')
  },

  // 超级管理员
  {
		path: '/superAdmin',
    name: 'superAdmin',
		component:() =>import ('../views/superAdmin/superAdmin.vue'),
    redirect: '/superAdmin/home',
		children: [{
      path: '/superAdmin/home',
      name: 'SAhome',
      component:() =>import ('../views/superAdmin/home.vue'),
			meta: [],
		},{
      path: '/superAdmin/stuInfManage',
      name: 'SIM',
      component:() =>import ('../views/superAdmin/stuInfManage.vue'),
      meta: ['数据管理', '学生信息管理'],
    },{
      path: '/superAdmin/teaInfManage',
      name: 'TIM',
      component:() =>import ('../views/superAdmin/teaInfManage.vue'),
      meta: ['数据管理', '教师信息管理'],
    },{
      path: '/superAdmin/queBankManage',
      name: 'QBM',
      component:() =>import ('../views/superAdmin/queBankManage.vue'),
      meta: ['数据管理', '题库管理'],
    },{
      path: '/superAdmin/sign',
      name: 'SAsign',
      component:() =>import ('../views/superAdmin/sign.vue'),
      meta: ['信息录入', '用户注册'],
    },{
      path: '/superAdmin/uploadQueBank',
      name: 'UQB',
      component:() =>import ('../views/superAdmin/uploadQueBank.vue'),
      meta: ['信息录入', '更新题库'],
    },]
  },

  // 管理员
  {
		path: '/admin',
    name: 'admin',
		component:() =>import ('../views/admin/admin.vue'),
    redirect: '/admin/home',
		children: [{
      path: '/admin/home',
      name: 'Ahome',
      component:() =>import ('../views/admin/home.vue'),
			meta: [],
		},{
      path: '/admin/generateTexePaper',
      name: 'GTP',
      component:() =>import ('../views/admin/generateTexePaper.vue'),
			meta: ['考试管理', '生成试卷'],
		},{
      path: '/admin/releaseExam',
      name: 'RE',
      component:() =>import ('../views/admin/releaseExam.vue'),
			meta: ['考试管理', '发布单科考试'],
		},{
      path: '/admin/integrationExam',
      name: 'IE',
      component:() =>import ('../views/admin/integrationExam.vue'),
			meta: ['考试管理', '整合考试'],
		},]
  },

  // 教师
  {
		path: '/teacher',
    name: 'teacher',
		component:() =>import ('../views/teacher/teacher.vue'),
    redirect: '/teacher/home',
		children: [{
      path: '/teacher/home',
      name: 'Thome',
      component:() =>import ('../views/teacher/home.vue'),
			meta: [],
		},{
      path: '/teacher/totalScoreView',
      name: 'TSV',
      component:() =>import ('../views/teacher/totalScoreView.vue'),
      meta: ['总成绩表'],
    },{
      path: '/teacher/subjectScoreView',
      name: 'SSV',
      component:() =>import ('../views/teacher/subjectScoreView.vue'),
      meta: ['学科成绩表'],
    },{
      path: '/teacher/createTopic',
      name: 'CT',
      component:() =>import ('../views/teacher/createTopic.vue'),
			meta: ['创造试题'],
		},{
      path: '/teacher/markTextPaper',
      name: 'MTP',
      component:() =>import ('../views/teacher/markTextPaper.vue'),
			meta: ['待批改试卷'],
		},{
      path: '/teacher/marking',
      name: 'M',
      component:() =>import ('../views/teacher/marking.vue'),
			meta: ['批改试卷'],
		},{
      path: '/teacher/subjectScoreTable',
      name: 'SST',
      component:() =>import ('../views/teacher/subjectScoreTable.vue'),
			meta: ['学科成绩表', '查看学科成绩表'],
		},{
      path: '/teacher/totalScoreTable',
      name: 'TST',
      component:() =>import ('../views/teacher/totalScoreTable.vue'),
			meta: ['总成绩表', '查看考试总成绩表'],
		},]
  },

  // 学生
  {
    path: '/student',
    name: 'student',
    component:() =>import ('../views/student/student.vue'),
    redirect: '/student/home',
    children: [{
      path: '/student/home',
      name: 'Shome',
      component:() =>import ('../views/student/home.vue'),
      meta: [],
    },{
      path: '/student/notStartExam',
      name: 'NSE',
      component:() =>import ('../views/student/notStartExam.vue'),
      meta: ['我的考试', '未开始考试'],
    },{
      path: '/student/startExam',
      name: 'SE',
      component:() =>import ('../views/student/startExam.vue'),
      meta: ['我的考试', '正在考试'],
    },{
      path: '/student/submitExam',
      name: 'submitE',
      component:() =>import ('../views/student/submitExam.vue'),
      meta: ['我的考试', '提交试卷'],
    },{
      path: '/student/completedExam',
      name: 'CE',
      component:() =>import ('../views/student/completedExam.vue'),
      meta: ['我的考试', '已结束考试'],
    },{
      path: '/student/viewTextPaper',
      name: 'VTP',
      component:() =>import ('../views/student/viewTextPaper.vue'),
      meta: ['我的考试', '已结束考试', '查看试卷'],
    },{
      path: '/student/collection',
      name: 'collection',
      component:() =>import ('../views/student/collection.vue'),
      meta: ['收藏'],
    },{
      path: '/student/viewCollectionTopic',
      name: 'VCT',
      component:() =>import ('../views/student/viewCollectionTopic.vue'),
      meta: [],
    },]
  },
]

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // from代表从哪个路径跳转而来
  // next 是一个函数，表示放行

  // next() 直接放行 next('/login')强制跳转到登录页面
  if (to.path === '/login') return next()

  // 判断用户是否有权限，强制跳转到登录页面
  let mypath = ''
  let newpath = to.path.split('/')[1]
  if (JSON.parse(sessionStorage.getItem('AuthorityJudgment')).permission == '超级管理员') mypath = ['superAdmin', 'admin', 'teacher']
  else if (JSON.parse(sessionStorage.getItem('AuthorityJudgment')).permission == '管理员') mypath = ['admin', 'teacher']
  else if (JSON.parse(sessionStorage.getItem('AuthorityJudgment')).permission == '教师') mypath = ['teacher']
  else if (JSON.parse(sessionStorage.getItem('AuthorityJudgment')).permission == '学生') mypath = ['student']
  for (let i = 0; i < mypath.length; i++) {
    if (mypath[i] == newpath) return next()
  }
  next('/login')
})


export default router
